package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.ChargeType;
import com.czh.springboot04.service.ChargeTypeService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chargeType")
@CrossOrigin
public class ChargeTypeController {
    @Autowired
    private ChargeTypeService chargeTypeService;

    @PostMapping("/insert")
    public R insert(@RequestBody ChargeType chargeType) {
        int i = chargeTypeService.insert(chargeType);
        if (i != 0) {
            return R.ok("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @PutMapping("/update")
    public R update(@RequestBody ChargeType chargeType) {
        int i = chargeTypeService.update(chargeType);
        if (i!= 0) {
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @DeleteMapping("/delete")
    public R delete(int id) {
        int i = chargeTypeService.delete(id);
        if (i!= 0) {
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @GetMapping("/selectByPage")
    public R selectByPage(int pageNum, int pageSize) {
        PageInfo<ChargeType> pageInfo = chargeTypeService.selectByPage(pageNum, pageSize);
        return R.ok(pageInfo);
    }

    @GetMapping("/selectById")
    public R selectById(int id) {
        ChargeType chargeType = chargeTypeService.selectById(id);
        return R.ok(chargeType);
    }
}
